//
//  ZYChangePhoneNumViewController.m
//  ZYOils
//
//  Created by 段新瑞 on 2019/10/4.
//  Copyright © 2019 Mr.Xr. All rights reserved.
//

#import "ZYChangePhoneNumViewController.h"
#import "LBTimeButton.h"

@interface ZYChangePhoneNumViewController () <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;


@property (weak, nonatomic) IBOutlet LBTimeButton *sendOldCodeBtn;
@property (weak, nonatomic) IBOutlet LBTimeButton *sendNewCodeBtn;

@property (weak, nonatomic) IBOutlet UITextField *oldCodeTextField;
@property (weak, nonatomic) IBOutlet UILabel *oldTitleLabel;

@property (weak, nonatomic) IBOutlet UITextField *nPhoneNumTextField;
@property (weak, nonatomic) IBOutlet UILabel *nTitleLabel;


@property (weak, nonatomic) IBOutlet UITextField *nPhoneCodeTextField;
@property (weak, nonatomic) IBOutlet UILabel *nCodeTitleLabel;

@end

@implementation ZYChangePhoneNumViewController

#pragma mark - Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configViews];
    [self configData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

}

#pragma mark - Setup View / Data

- (void)configViews {
    
    self.titleLabel.text = [NSString stringWithFormat:@"点击发送验证码获取%@手机号验证码", ZYUserInfoModel.sharedInfoModel.userModel.phone];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange:) name:UITextFieldTextDidChangeNotification object:nil];
    
    
    @weakify(self)
    [[self.sendOldCodeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        NSDictionary *dict = @{@"phone": STR(ZYUserInfoModel.sharedInfoModel.userModel.phone),
                               @"type": @"changePhoneNO"};
        [ZYHud showLoadingText:@""];
        [[DataManager manager] postPhoneCodeParam:dict block:^(XRCommonModel *model, NSError *error) {
            [ZYHud hideHud];
            if (model.isSuccess) {
                [self.sendOldCodeBtn startTime];
            } else {
                if (![LBLimitInput isEmpty:model.error]) {
                    [ZYHud showErrorText:model.error];
                }
            }
        }];
    }];
    
    [[self.sendNewCodeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        if ([LBLimitInput isEmpty:self.nPhoneNumTextField.text]) {
            [ZYHud showErrorText:@"请输入新手机号"];
            return;
        }
        NSDictionary *dict = @{@"phone": STR(self.nPhoneNumTextField.text),
                               @"type": @"changePhoneNO"};
        [ZYHud showLoadingText:@""];
        [[DataManager manager] postPhoneCodeParam:dict block:^(XRCommonModel *model, NSError *error) {
            [ZYHud hideHud];
            if (model.isSuccess) {
                [self.sendNewCodeBtn startTime];
            } else {
                if (![LBLimitInput isEmpty:model.error]) {
                    [ZYHud showErrorText:model.error];
                }
            }
        }];
    }];
    
}

- (void)textChange:(NSNotification *)sender {
    UITextField *object = sender.object;
    if ([object isEqual:self.oldCodeTextField]) {
        self.oldTitleLabel.hidden = object.text.length == 0 ? NO : YES;
    } else if([object isEqual:self.nPhoneNumTextField]) {
        self.nTitleLabel.hidden = object.text.length == 0 ? NO : YES;
    } else {
        self.nCodeTitleLabel.hidden = object.text.length == 0 ? NO : YES;
    }
}

- (IBAction)confirmBtnClick:(id)sender {
    if ([LBLimitInput isEmpty:self.nPhoneNumTextField.text]) {
        [ZYHud showErrorText:@"请输入新手机号"];
        return;
    }
    
    if ([LBLimitInput isEmpty:self.oldCodeTextField.text]) {
        [ZYHud showErrorText:@"请输入旧手机号验证码"];
        return;
    }
    
    if ([LBLimitInput isEmpty:self.nPhoneNumTextField.text]) {
        [ZYHud showErrorText:@"请输入新手机号"];
        return;
    }
    
    if ([LBLimitInput isEmpty:self.nPhoneCodeTextField.text]) {
        [ZYHud showErrorText:@"请输入新手机号验证码"];
        return;
    }
    
    NSDictionary *dict = @{@"userId": STR(ZYUserInfoModel.sharedInfoModel.userModel.userId),
                           @"newPhone": STR(self.nPhoneNumTextField.text),
                           @"newPhoneCode": STR(self.nPhoneCodeTextField.text),
                           @"oldPhone": STR(ZYUserInfoModel.sharedInfoModel.userModel.phone),
                           @"oldPhoneCode": STR(self.oldCodeTextField.text),
                           @"type": @"changePhoneNO"};
    
    [ZYHud showLoadingText:@""];
    [[DataManager manager] postUpdatePhoneParam:dict block:^(XRCommonModel *model, NSError *error) {
        [ZYHud hideHud];
        if (model.isSuccess) {
            [ZYHud showSuccessText:@"手机号修改成功"];
            ZYUserInfoModel.sharedInfoModel.userModel.phone = self.nPhoneNumTextField.text;
        } else {
            if (![LBLimitInput isEmpty:model.error]) {
                [ZYHud showErrorText:model.error];
            }
        }
    }];
}

              
//当前对像要销毁的时候，创建的通知要进行移除：（否则可能报错，坏内存访问）
-(void)dealloc{
  [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)configData {

    
    
}

#pragma mark - Observer

#pragma mark - Notification

#pragma mark - Event Response

#pragma mark - Override Methods

#pragma mark - Delegate

#pragma mark - Public Methods

#pragma mark - Private Methods

#pragma mark - Setter / Getter

#pragma mark - Network




@end
